#include "settingseditor.h"
#include "ui_settingseditor.h"
#include <QTest>

SettingsEditor::SettingsEditor(QWidget *parent) :
    KeyControllableMenu(parent),
    m_ui(new Ui::SettingsEditor)
{
    setMouseTracking(true);
    m_ui->setupUi(this);
    WindowManager::addWidget(this);
    mainLayout=new QVBoxLayout(m_ui->verticalLayout);
    //m_ui->menu->setLayout(mainLayout);
    m_ui->settingsEdit->setLayout(new QVBoxLayout());
    currentMenu=NULL;
    initializeButtons();
}

SettingsEditor::~SettingsEditor()
{
    delete m_ui;
}

void SettingsEditor::initializeButtons() {
    shortcuts = new ShortcutSettings(this);
    addItem(QIcon(":/imgs/3F00Button_Shortcuts.png"),"Shortcuts",shortcuts);
    shortcuts->hide();
    connect(shortcuts,SIGNAL(completed()), this, SLOT(reshowComponents()));
    playbackShortcuts=new PlaybackShortcutSettings(this);
    addItem(QIcon(":/imgs/3F00Button_PlaybackShortcuts.png"),"Playback Shortcuts",playbackShortcuts);
    playbackShortcuts->hide();
    connect(playbackShortcuts,SIGNAL(completed()), this, SLOT(reshowComponents()));
    //formats= new FormatSettings(this);
    //addItem(QIcon(":/imgs/3F00Button_Formats.png"),"Formats",formats);
    //formats->hide();
    //connect(formats,SIGNAL(completed()), this, SLOT(reshowComponents()));
    mediaLocator=new MediaLocatorSettings(this);
    addItem(QIcon(":/imgs/3F00Button_MediaLocator.png"),"Media Locator",mediaLocator);
    mediaLocator->hide();
    connect(mediaLocator,SIGNAL(completed()), this, SLOT(reshowComponents()));
    themes=new ThemeSettings(this);
    addItem(QIcon(":/imgs/3F00Button_Themes.png"),"Themes",themes);
    themes->hide();
    connect(themes,SIGNAL(completed()), this, SLOT(reshowComponents()));
    //shortcuts->hide();
}

void SettingsEditor::addItem(QIcon icon, QString name, SettingsMenuItem * item ) {
    Button * button=new Button(icon, "",this,item);
    button->setMinimumWidth(256);
    button->setIconSize(QSize(256,64));
    m_ui->menu->addWidget(button,0,Qt::AlignTop|Qt::AlignLeft);
    addRow(button);
    m_ui->settingsEdit->layout()->addWidget(item);
}

void SettingsEditor::buttonSelected() {
    SettingsMenuItem * newWidget=qobject_cast<SettingsMenuItem *>(getCurrentButton()->getItem());
    if(newWidget!=NULL) {
        if(currentMenu!=NULL) {
            currentMenu->hide();
        }
        currentMenu=newWidget;
        currentMenu->show();
        deactivate();
        newWidget->activate();
    }
}

void SettingsEditor::reshowComponents() {
    if(currentMenu) {
        currentMenu->deactivate();
        currentMenu->hide();
        //KeyControllableMenu::queue.removeLast();
    }

    activate();
}

void SettingsEditor::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void SettingsEditor::test() {
    qDebug() <<m_ui->verticalLayout->size();
    QList<SettingsMenuItem *> * list=new QList<SettingsMenuItem *>();
    list->append(shortcuts);
    list->append(formats);
    foreach(SettingsMenuItem * item, *list) {
        Button * button=findButton(item);
        if(button) {
            QTest::mouseMove(button);
            emit buttonEntered(button);
            QTest::qWait(1000);
            QTest::mouseClick(button, Qt::LeftButton);
            QTest::qWait(1000);
            item->saveTest();
            QTest::mouseMove(button);
            QTest::qWait(2000);
            QTest::mouseClick(button, Qt::LeftButton);
            item->assertSaved();
            QTest::qWait(1000);
            QTest::mouseClick(button, Qt::LeftButton);
            item->cancelTest();
            QTest::qWait(1000);
            item->assertCancelled();
        }
    }
}
